package com.xinjian.jianba.dao.dwmid;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;
import org.omg.CORBA.PUBLIC_MEMBER;

import com.xinjian.jianba.dto.ActivityFindOneDto;
import com.xinjian.jianba.dto.ActivityPersonIndexVo;
import com.xinjian.jianba.dto.AllRankDto;
import com.xinjian.jianba.dto.TicketsDto;
import com.xinjian.jianba.dto.UserActivityDto;
import com.xinjian.jianba.enity.AcwardsEntity;
import com.xinjian.jianba.enity.AllRankEntity;
import com.xinjian.jianba.enity.UserActivityEntity;
import com.xinjian.jianba.enity.UserMediaActivityEntity;
import com.xinjian.jianba.enity.UserMediaEnity;

@Mapper
public interface TicketsDao {

	@Insert("insert into tickets (userid,matchid,workid,addtime) values (#{userId},#{matchId},#{workId},now())")
	public int vote(TicketsDto ticketsDto);
	
	@Insert("insert into user_activity (activityName,activityLogo,detail,registerid,addtime) values(#{userActivityEntity.activityName},#{userActivityEntity.activityLogo},#{userActivityEntity.detail},#{userActivityEntity.registerId},now())")
	@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "userActivityEntity.userId", before = false, resultType = Long.class)
	public Long join(@Param("userActivityEntity")UserActivityEntity userActivityEntity);
	
	@Update("update user_activity set activityName=#{activityName},detail=#{detail} where registerid=#{registerId}")
	public int editor(UserActivityDto userActivityDto);
	
	@Insert("insert into user_media (uid,address,mtype,cre_time,time_add,type,imgStr) values(#{uid},#{address},#{mtype},now(),now(),#{type},#{imgStr})")
	public int workUp(UserMediaEnity userMediaEnity);
	
	@Select("select count(*) from user_media where videoId=#{videoId} and uid is not null")
	public int isUped(@Param("videoId")String videoId); 
	
	@Update("update user_media set uid=#{uid},address=#{address},mtype=#{mtype},cre_time=now(),time_add=now(),type=#{type},imgStr=#{imgStr} where videoId=#{videoId}")
	public int updateWork(UserMediaEnity userMediaEnity);
	
	@Select("select registerid,userid,activityName,activityLogo,detail from user_activity where registerid=#{registerId}")
	@Results({
		@Result(id=true,column="userid",property="activityUid"),
		@Result(column="registerid",property="registerid"),
		@Result(column="activityName",property="activityName"),
		@Result(column="activityLogo",property="activityLogo"),
		@Result(column="detail",property="detail"),
		@Result(column="registerid" ,property="activityWorks",many=@Many(
		            select="com.xinjian.jianba.dao.dwmid.TicketsDao.findWorksByRegisterid",
		            fetchType=FetchType.EAGER))
		
	})
	public ActivityPersonIndexVo findPersonIndex(@Param("registerId") Long registerId);
	
	@Select("select id workId,mp4Url,videoId,(select count(*) from tickets where workid=user_media.id) counts,imgStr from user_media where uid=#{registerId} and mtype='activityVod' order by cre_time asc")
	public List<UserMediaActivityEntity> findWorksByRegisterid(Long registerId);
	
	@Select("select rank,num ticketCount from (select matchid,num,@rownum:=(@rownum+1) rank from (select matchid,num from (select matchid,sum(num) num from (select matchid,num from (select matchid,num, @rn:=IF(@prev=matchid,@rn+1,1) AS rn,@prev:=matchid from(select matchid,num from (select matchid,count(*) num from tickets group by matchid,workid) t order by matchid asc,num desc) h JOIN (select @rn:=0 ) as vars) temp where rn<6) temp1 group by matchid) temp2 order by num desc) temp3 join (select @rownum:=0) as temp4) temp5 where matchid=#{registerId}")
	public Map<String, Object> getTicketsCountAndRank(Long registerId);
	
	@Select("select count(*) from user_activity where registerid=#{registerId}")
	public int isJoin(Long registerId);
	
	@Select("select (select userid from user_activity where registerid=matchid) activityUid,(select activityName from user_activity where registerid=matchid) activityName,(select activityLogo from user_activity where registerid=matchid) activityLogo,matchid registerid,num count from (select matchid,sum(num) num from (select matchid,num from (select matchid,num, @rn:=IF(@prev=matchid,@rn+1,1) AS rn,@prev:=matchid from(select matchid,num from (select matchid,count(*) num from tickets group by matchid,workid) t order by matchid asc,num desc) h JOIN (select @rn:=0 ) as vars) temp where rn<6) temp1 group by matchid) temp2 order by num desc limit #{offset},#{pageSize}")
	public List<AllRankEntity> allRank(AllRankDto allRankDto); 
	
	@Select("select (select userid from user_activity where registerid=matchid) activityUid,(select activityName from user_activity where registerid=matchid) activityName,(select activityLogo from user_activity where registerid=matchid) activityLogo,matchid registerid,num count from (select matchid,sum(num) num from (select matchid,num from (select matchid,num, @rn:=IF(@prev=matchid,@rn+1,1) AS rn,@prev:=matchid from(select matchid,num from (select matchid,count(*) num from tickets group by matchid,workid) t order by matchid asc,num desc) h JOIN (select @rn:=0 ) as vars) temp where rn<6) temp1 group by matchid) temp2 WHERE matchid=(select registerid from user_activity where userid=#{activityNameOrActivityUid} or activityName=#{activityNameOrActivityUid}) ")
	public AllRankEntity findOne(ActivityFindOneDto activityFindOneDto);
	
	@Select("select count(*) from user_media where uid=#{registerId} and mtype='activityVod'")
	public int isFull(Long registerId);
	
	@Delete("delete from tickets where workid=#{workId}")
	public int deleteWorkTickets(Long workId);
	
	@Delete("delete from user_media where id=#{workId}")
	public int deleteUserMedia(Long workId);
	
	@Select("select videoId from user_media where id=#{workId}")
	public String findVideoIdByWorkId(Long workId);
	
	@Select("select activityLogo from user_activity where registerid=#{registerId}")
	public String findByRegisterId(Long registerId);
	
	@Update("update user_activity set activityLogo=#{address} where registerid=#{userId}")
	public int updateUplogo(Map<String, Object> map);
	
	@Update("update user_activity set activityName=#{activityName},detail=#{detail},activityLogo=#{base64Imgstr} where registerid=#{registerId}")
	public int editorAndLogo(UserActivityDto userActivityDto);
	
	@Select("select wardid,mobile,nickname,money,content,add_date,add_time from acwards")
	public List<AcwardsEntity> getLuckPersons();
	
	
	
}
